package com.company.huisuo.utils

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.view.View
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsControllerCompat

object StatusBarUtil {

    /**
     * 让内容延伸到状态栏区域，实现沉浸式布局
     *
     * @param activity 目标 Activity
     * @param isLightStatusBar true = 黑色图标（浅色背景下）; false = 白色图标（深色背景下）
     */
    fun immersiveStatusBar(
        activity: Activity,
        isLightStatusBar: Boolean = false
    ) {
        val window = activity.window

        //让布局延伸至状态栏区域
        WindowCompat.setDecorFitsSystemWindows(window, false)

        //设置状态栏透明
        window.statusBarColor = Color.TRANSPARENT

        //设置状态栏图标颜色（亮/暗）
        val controller = WindowInsetsControllerCompat(window, window.decorView)
        controller.isAppearanceLightStatusBars = isLightStatusBar

        //保证旧系统下稳定布局
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {

            val value = if (isLightStatusBar) {
                View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            } else {
                0
            }

            window.decorView.systemUiVisibility =
                (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or value)
        }
    }

    /**
     * 恢复默认状态栏样式（内容不再延伸）
     */
    fun resetStatusBar(
        activity: Activity,
        color: Int = Color.WHITE,
        isLightStatusBar: Boolean = true
    ) {
        val window = activity.window
        WindowCompat.setDecorFitsSystemWindows(window, true)
        window.statusBarColor = color

        val controller = WindowInsetsControllerCompat(window, window.decorView)
        controller.isAppearanceLightStatusBars = isLightStatusBar
    }
}